package cn.stylefeng.guns.modular.transport.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.util.DateUtil;
import cn.stylefeng.guns.modular.basic.service.BasicCustomerService;
import cn.stylefeng.guns.modular.basic.service.BasicXlbOauth2tokenService;
import cn.stylefeng.guns.modular.order.mapper.OrderTransportMapper;
import cn.stylefeng.guns.modular.order.model.params.OrderTransportParam;
import cn.stylefeng.guns.modular.order.model.result.OrderTransportResult;
import cn.stylefeng.guns.modular.rest.aop.UserAop;
import cn.stylefeng.guns.modular.transport.entity.TransportPlateStore;
import cn.stylefeng.guns.modular.transport.mapper.TransportPlateStoreMapper;
import cn.stylefeng.guns.modular.transport.model.params.TransportPlateStoreParam;
import cn.stylefeng.guns.modular.transport.model.result.TransportPlateStoreResult;
import  cn.stylefeng.guns.modular.transport.service.TransportPlateStoreService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.Serializable;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 绑定门店 服务实现类
 * </p>
 *
 * @author zhangzh
 * @since 2024-06-12
 */
@Service
public class TransportPlateStoreServiceImpl extends ServiceImpl<TransportPlateStoreMapper, TransportPlateStore> implements TransportPlateStoreService {

    @Autowired
    private UserAop userAop;

    @Resource
    private OrderTransportMapper orderTransportMapper;

    @Autowired
    private BasicCustomerService basicCustomerService;

    @Autowired
    private BasicXlbOauth2tokenService basicXlbOauth2tokenService;

    @Value("${hxl.api.url}")
    private String hxlApiUrl;

    @Override
    public void add(TransportPlateStoreParam param){
        TransportPlateStore entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(TransportPlateStoreParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(TransportPlateStoreParam param){
        TransportPlateStore oldEntity = getOldEntity(param);
        TransportPlateStore newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public void setOrd(TransportPlateStoreParam param) {
        TransportPlateStore oldEntity = getOldEntity(param);
        TransportPlateStore newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public TransportPlateStoreResult findBySpec(TransportPlateStoreParam param){
        return null;
    }

    @Override
    public List<TransportPlateStoreResult> findListBySpec(TransportPlateStoreParam param){
        return this.baseMapper.customList(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(TransportPlateStoreParam param,String condition){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param,condition);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public ResponseData noBindList(Long operateIdc, OrderTransportParam orderTransportParam) {
        List<OrderTransportResult> listRs = new ArrayList<>();
        Map<String,String> plMap = new HashMap<>();
        User user = userAop.getUser();
        if(ToolUtil.isNotEmpty(orderTransportParam.getSetMentiontime())) {//查询前三日甩货
            try {
                orderTransportParam.setTimeInterval("'"+ DateUtil.getPastDateZd(orderTransportParam.getSetMentiontime(),3)+"' and '"+orderTransportParam.getSetMentiontime()+"'");
                List<OrderTransportResult> list = this.orderTransportMapper.listgetUnitNewNew(orderTransportParam,operateIdc);
                TransportPlateStoreParam param = new TransportPlateStoreParam();
                param.setUserId(user.getUserId());
                List<TransportPlateStoreResult> transportPlateStoreResults = this.baseMapper.customList(param);
                for(TransportPlateStoreResult _transportPlateStoreResult : transportPlateStoreResults){
                    plMap.put(_transportPlateStoreResult.getPutGoodsId(),_transportPlateStoreResult.getPutGoodsName());
                }
                for(OrderTransportResult _orderTransportResult : list){
                    if(plMap.get(_orderTransportResult.getXlbClientId()) == null){
                        listRs.add(_orderTransportResult);
                    }
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return ResponseData.success(listRs);
    }

    private Serializable getKey(TransportPlateStoreParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private TransportPlateStore getOldEntity(TransportPlateStoreParam param) {
        return this.getById(getKey(param));
    }

    private TransportPlateStore getEntity(TransportPlateStoreParam param) {
        TransportPlateStore entity = new TransportPlateStore();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
